﻿using System;
using System.Configuration;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Xml.XPath;
using System.Collections;
using System.Collections.ObjectModel;

using XmlVisualizer.Models;
using XmlVisualizer.Controllers;
using XmlVisualizer.Views;
using DiagramDesigner;
using DiagramDesigner.Controls;

namespace XMLVisualizer
{
    // System.Windows.Forms.OpenFileDialog openfile = new System.Windows.Forms.OpenFileDialog();
    // openfile.ShowDialog();

    /// <summary>
    /// Represents the main window
    /// </summary>
    public partial class Document : Window, IView, Observer
    {
        #region M E M B E R S

        private IController appController; // application controller
        private ApplicationModel appModel; // application model
        private DocumentReference docRef; // reference to the document
        private ObservableCollection<VisualNode> visualNodes = new ObservableCollection<VisualNode>(); // collection of the visual nodes (XML documents)

        private ObservableCollection<Ref> refs = new ObservableCollection<Ref>(); // references we need to connect after the reading of XSD schema is complete
        private Hashtable namesToDesignerItemHT = new Hashtable(); // hashtable containing all the XmlQualifiedName(s) linked to DesignerItem(s)

        #endregion

        #region M E T H O D S

        /// <summary>
        /// Constructor of the main window, creates model, view and controller.
        /// Additionally registers observers of the model.
        /// </summary>
        public Document()
        {
            InitializeComponent();
            MakeCommandBindings();

            IView view = this as IView;
            appModel = new ApplicationModel(view);
            appController = new ApplicationController(ref appModel, ref view);
            appModel.RegisterObserver((Observer)this);
        }

        /// <summary>
        /// Loads the file.
        /// </summary>
        /// <param name="docRef"></param>
        public void LoadFile(DocumentReference docRef)
        {
            this.docRef = docRef;
            this.appController.View_OpenFile(docRef.Name);

            char[] delim = { '\\', '/' };
            String[] shortTitle = docRef.Name.Split(delim);

            // set the window title            
            this.Title = shortTitle[shortTitle.Length - 1] + " - " + this.Title;
        }

        /// <summary>
        /// Handles the event when the document window gets closed. The reference
        /// to that document is then removed from the list of open documents.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Document_Closed(object sender, EventArgs e)
        {
            ((MainApp)Application.Current).Documents.Remove(docRef);
            this.Close();
        }

        #region COMMANDS
        /// <summary>
        /// Creates bindings for standard command.
        /// </summary>
        private void MakeCommandBindings()
        {
            // Window Close
            this.Closed += new EventHandler(Document_Closed);

            // New command
            CommandBinding bindingNew = new CommandBinding(ApplicationCommands.New);
            bindingNew.Executed += NewCommand_Executed;
            this.CommandBindings.Add(bindingNew);

            // Open command
            CommandBinding bindingOpen = new CommandBinding(ApplicationCommands.Open);
            bindingOpen.Executed += OpenCommand_Executed;
            this.CommandBindings.Add(bindingOpen);

            // Save command
            CommandBinding bindingSave = new CommandBinding(ApplicationCommands.Save);
            bindingSave.Executed += SaveCommand_Executed;
            this.CommandBindings.Add(bindingSave);

            // Save As command
            CommandBinding bindingSaveAs = new CommandBinding(ApplicationCommands.SaveAs);
            bindingSaveAs.Executed += SaveAsCommand_Executed;
            this.CommandBindings.Add(bindingSaveAs);

            // Close command
            CommandBinding bindingClose = new CommandBinding(ApplicationCommands.Close);
            bindingClose.Executed += CloseCommand_Executed;
            this.CommandBindings.Add(bindingClose);

            // Exit command
            CommandBinding bindingExit = new CommandBinding(MyCommands.Exit);
            bindingExit.Executed += ExitCommand_Executed;
            this.CommandBindings.Add(bindingExit);

            // Templates command
            CommandBinding bindingTemplates = new CommandBinding(MyCommands.Templates);
            bindingTemplates.Executed += TemplatesCommand_Executed;
            this.CommandBindings.Add(bindingTemplates);
        }

        // File -> New
        private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Document doc = new Document();
            doc.Show();
        }

        // File -> Open
        private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            OpenFile();
        }

        // File -> Save
        private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Save command triggered by " + e.Source.ToString());
        }

        // File -> Save As
        private void SaveAsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Save As command triggered by " + e.Source.ToString());
        }

        // File -> Close
        private void CloseCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // do some clean up before...
            this.Close();
            //Application.Current.Shutdown();
        }

        // File -> Exit
        private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // do some clean up before...
            Application.Current.Shutdown();
        }

        // temporary command
        private void TemplatesCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // show the other window
            Templates tempWin = new Templates();
            tempWin.Show();
        }

        #endregion

        #endregion

        #region IView Implementation

        public void NewFile()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Invokes the OpenFileDialog, user selects the file to open and application loads the file.
        /// </summary>
        public void OpenFile()
        {
            // opens the OpenFileFialog, gets a filename and sends this information to the controller
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = false;

            ofd.Filter = "XML Documents (*.xml)|*.xml;|XML Schema (*.xsd)|*.xsd|All Files|*.*";

            string filePath;
            string safeFilePath;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filePath = ofd.FileName;
                safeFilePath = ofd.SafeFileName;

                ((MainApp)Application.Current).ShowDocument(filePath);
            }
        }

        public void SaveFile()
        {
            throw new NotImplementedException();
        }

        public void Undo()
        {
            throw new NotImplementedException();
        }

        public void Redo()
        {
            throw new NotImplementedException();
        }

        public void Settings()
        {
            throw new NotImplementedException();
        }

        public void Exit()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Observer Implementation

        public void notify()
        {
            throw new NotImplementedException();
        }

        #region XML PART

        public void DrawXml()
        {
            XmlModel xmlModel = this.appModel.XMLModel;
            ObservableCollection<VisualNode> visualNodes = xmlModel.VisualNodes;
            foreach (VisualNode node in visualNodes)
                node.Rendered = false;

            XmlNode root = xmlModel.documentElement;
            VisualNode visualRoot = xmlModel.getVisualFromNode(ref root);
            Queue queueV = new Queue();
            Queue queueD = new Queue();

            DesignerItem newItem = createVisualElement(new Point(0, 10), visualRoot);

            queueV.Enqueue(visualRoot);
            queueD.Enqueue(newItem);

            double deltax = 0;
            double deltay = 150;
            int countX = 0;
            int countY = 0;

            Double width = 0;
            int lastDepth = 0;

            while (queueV.Count > 0)
            {
                VisualNode parent = queueV.Dequeue() as VisualNode;
                DesignerItem DIparent = queueD.Dequeue() as DesignerItem;

                if (parent.Depth > lastDepth)
                    deltax = 0;

                lastDepth = parent.Depth;

                Control connectorDecorator = DIparent.Template.FindName("PART_ConnectorDecorator", DIparent) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                foreach (VisualNode node in parent.Children)
                {
                    if (node.Data.NodeType != XmlNodeType.Whitespace && node.Rendered == false)
                    {
                        node.Depth = parent.Depth + 1;
                        node.Rendered = true;

                        DesignerItem child = createVisualElement(new Point(10 + deltax, 10 + node.Depth * deltay), node);

                        queueV.Enqueue(node);
                        queueD.Enqueue(child);

                        Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", child) as Control;
                        childConnectorDecorator.ApplyTemplate();

                        Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                        Connection conn = new Connection(bottom, top);
                        Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                        designerCanvas.Children.Add(conn);

                        deltax += 160;
                        countX++;
                        if (deltax > width)
                            width = deltax;
                        if (countX > 14)
                            break;
                    }
                }

                countY++;
                if (parent.Depth == 6)
                    break;
            }

            double center = (width + 10) / 2;
            Canvas.SetLeft(newItem, center);
        }

        public DesignerItem createVisualElement(Point position, VisualNode node)
        {
            DesignerItem newItem = new DesignerItem(node.AppearanceTemplate, node.ConnectorDecoratorTemplate);
            newItem.Padding = new Thickness(10);
            newItem.Width = 150;

            ObservableCollection<DiagramDesigner.Properties> properties = new ObservableCollection<DiagramDesigner.Properties>();

            if (node.Data.NodeType == XmlNodeType.Element)
            {
                newItem.elementType = node.Data.NodeType.ToString();
                newItem.elementValue = node.Data.Name.ToString();

                if (node.Attributes.Count != 0)
                {
                    for (int i = 0; i < node.Attributes.Count; i++)
                    {
                        properties.Add(new DiagramDesigner.Properties { Name = node.Attributes.visualAttributes[i].Data.Name, Value = node.Attributes.visualAttributes[i].Data.Value });
                    }
                    newItem.ToolTip = "Element has attributes.";
                }
            }
            else if (node.Data.NodeType == XmlNodeType.Whitespace)
            {
                // we ignore whitespace for now...
            }
            else
            {
                newItem.elementType = node.Data.NodeType.ToString();
                newItem.elementValue = node.Data.Value.ToString();
            }

            DesignerItemContent c = new DesignerItemContent();
            DesignerItemContent.SetElementType(c, newItem.elementType);
            DesignerItemContent.SetElementValue(c, newItem.elementValue);
            DesignerItemContent.SetContentProperties(c, properties);
            c.Set_PART_ElementValue_Template(FindResource("ElementValueControlRead") as ControlTemplate, true);

            newItem.Content = c;

            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            return newItem;
        }

        #endregion

        #region XSD PART

        protected DesignerItem CreateVisualXsdElement(Point position, ref XmlSchemaObject element)
        {
            if (element.GetType() == typeof(XmlSchemaVisualAnnotation))
            {
                return CreateXmlSchemaVisualAnnotation(position, ref element); // done
            }
            else if (element.GetType() == typeof(XmlSchemaVisualAttribute))
            {
                return CreateXmlSchemaVisualAttribute(position, ref element); // almost done
            }
            else if (element.GetType() == typeof(XmlSchemaVisualAttributeGroup))
            {
                return CreateXmlSchemaVisualAttributeGroup(position, ref element); // almost done
            }
            else if (element.GetType() == typeof(XmlSchemaVisualComplexType))
            {
                return CreateXmlSchemaVisualComplexType(position, ref element);
            }
            else if (element.GetType() == typeof(XmlSchemaVisualSimpleType))
            {
                return CreateXmlSchemaSimpleType(position, ref element); // almost done
            }
            else if (element.GetType() == typeof(XmlSchemaVisualElement))
            {
                return CreateXmlSchemaElement(position, ref element);
            }
            else if (element.GetType() == typeof(XmlSchemaVisualGroup))
            {
                return CreateXmlSchemaGroup(position, ref element);
            }
            else if (element.GetType() == typeof(XmlSchemaVisualNotation))
            {
                return CreateXmlSchemaNotation(position, ref element); // done
            }

            return null;
        }

        protected DesignerItem CreateXmlSchemaVisualAnnotation(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualAnnotation.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualAnnotation.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAnnotation visualAnnotation = element as XmlSchemaVisualAnnotation;

            AnnotationContent content = new AnnotationContent();

            content.Id = visualAnnotation.SchemaAnnotation.Id;
            if (content.Id == null)
                content.DisableContentControl("PART_Id");
            else
            {
                content.Id = "Id: " + visualAnnotation.SchemaAnnotation.Id;
            }

            content.Items = new ObservableCollection<Items>();
            if (visualAnnotation.SchemaAnnotation.Items != null)
            {
                foreach (XmlSchemaObject obj in visualAnnotation.SchemaAnnotation.Items)
                {
                    if (obj.GetType() == typeof(XmlSchemaDocumentation))
                    {
                        XmlSchemaDocumentation documentation = obj as XmlSchemaDocumentation;
                        foreach (XmlNode node in documentation.Markup)
                        {
                            content.Items.Add(new DiagramDesigner.Items { InnerText = node.InnerText });
                        }
                    }
                    else if (obj.GetType() == typeof(XmlSchemaAppInfo))
                    {
                        XmlSchemaAppInfo appInfo = obj as XmlSchemaAppInfo;
                        foreach (XmlNode node in appInfo.Markup)
                        {
                            content.Items.Add(new DiagramDesigner.Items { InnerText = node.InnerText });
                        }
                    }
                }
            }
            else
            {
                content.DisableContentControl("PART_Items");
            }

            content.LineNumber = "LineNumber: " + visualAnnotation.SchemaAnnotation.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualAnnotation.SchemaAnnotation.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualAnnotation.SchemaAnnotation.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualAnnotation.SchemaAnnotation.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
            {
                content.DisableContentControl("PART_Namespaces");
            }
            content.Parent = "Parent: " + visualAnnotation.SchemaAnnotation.Parent.GetType().ToString();
            content.SourceUri = "SourceUri: " + visualAnnotation.SchemaAnnotation.SourceUri;

            if (visualAnnotation.SchemaAnnotation.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualAnnotation.SchemaAnnotation.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;

            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaVisualAttribute(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                        FindResource(XmlSchemaVisualAttribute.defaultAppearanceTemplate) as ControlTemplate,
                        FindResource(XmlSchemaVisualAttribute.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAttribute visualAttribute = element as XmlSchemaVisualAttribute;

            AttributeContent content = new AttributeContent();

            // Name
            if (visualAttribute.SchemaAttribute.Name != null)
                content.Name = "Name: " + visualAttribute.SchemaAttribute.Name;
            else
                content.DisableContentControl("PART_Name");

            // Default Value
            if (visualAttribute.SchemaAttribute.DefaultValue != null)
                content.DefaultValue = "DefaultValue: " + visualAttribute.SchemaAttribute.DefaultValue;
            else
                content.DisableContentControl("PART_DefaultValue");

            // Fixed Value
            if (visualAttribute.SchemaAttribute.FixedValue != null)
                content.FixedValue = "FixedValue: " + visualAttribute.SchemaAttribute.FixedValue;
            else
                content.DisableContentControl("PART_FixedValue");

            // Form
            if (visualAttribute.SchemaAttribute.Form != null)
                content.Form = "Form: " + visualAttribute.SchemaAttribute.Form.ToString();
            else
                content.DisableContentControl("PART_Form");

            // Id
            if (visualAttribute.SchemaAttribute.Id != null)
                content.Id = "Id: " + visualAttribute.SchemaAttribute.Id;
            else
                content.DisableContentControl("PART_Id");

            // Line number
            content.LineNumber = "LineNumber: " + visualAttribute.SchemaAttribute.LineNumber.ToString();

            // Line position
            content.LinePosition = "LinePosition: " + visualAttribute.SchemaAttribute.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (visualAttribute.SchemaAttribute.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualAttribute.SchemaAttribute.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            if (visualAttribute.SchemaAttribute.Parent != null)
                content.Parent = "Parent: " + visualAttribute.SchemaAttribute.Parent.GetType().ToString();
            else
                content.DisableContentControl("PART_Parent");

            // Qualified Name
            if (visualAttribute.SchemaAttribute.QualifiedName != null)
                content.QualifiedName = "QualifiedName: " + visualAttribute.SchemaAttribute.QualifiedName.Name;
            else
                content.DisableContentControl("PART_QualifiedName");

            // Ref Name
            if (visualAttribute.SchemaAttribute.RefName != null)
                content.RefName = "RefName: " + visualAttribute.SchemaAttribute.RefName.Name;
            else
                content.DisableContentControl("PART_RefName");

            // SourceUri
            content.SourceUri = "SourceUri: " + visualAttribute.SchemaAttribute.SourceUri;

            // Unhandled Attributes
            if (visualAttribute.SchemaAttribute.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualAttribute.SchemaAttribute.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            // Use
            content.Use = "Use: " + visualAttribute.SchemaAttribute.Use.ToString();

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (visualAttribute.Annotation != null)
            {
                XmlSchemaObject annotation = visualAttribute.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100 + 30.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            // create new visual object for simple type and connect it
            if (visualAttribute.AttributeSchemaType != null)
            {

                XmlSchemaObject attributeSchemaType = visualAttribute.AttributeSchemaType;
                Point newPosition = new Point(position.X, position.Y + 200 + 60);
                DesignerItem DIattributeSchemaType = CreateXmlSchemaSimpleType(newPosition, ref attributeSchemaType);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIattributeSchemaType) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);

            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaVisualAttributeGroup(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualAttributeGroup.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualAttributeGroup.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAttributeGroup visualAttributeGroup = element as XmlSchemaVisualAttributeGroup;

            AttributeGroupContent content = new AttributeGroupContent();

            content.Name = "Name: " + visualAttributeGroup.SchemaAttributeGroup.Name;
            content.Id = "Id: " + visualAttributeGroup.SchemaAttributeGroup.Id;
            content.LineNumber = "LineNumber: " + visualAttributeGroup.SchemaAttributeGroup.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualAttributeGroup.SchemaAttributeGroup.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualAttributeGroup.SchemaAttributeGroup.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualAttributeGroup.SchemaAttributeGroup.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualAttributeGroup.SchemaAttributeGroup.Parent.GetType().ToString();
            content.QualifiedName = "QualifiedName: " + visualAttributeGroup.SchemaAttributeGroup.QualifiedName.Name;
            content.SourceUri = "SourceUri: " + visualAttributeGroup.SchemaAttributeGroup.SourceUri;

            if (visualAttributeGroup.SchemaAttributeGroup.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualAttributeGroup.SchemaAttributeGroup.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;

            // create new visual object for annotation and connect it
            if (visualAttributeGroup.Annotation != null)
            {
                XmlSchemaObject annotation = visualAttributeGroup.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create new visual objects for Attributes
            if (visualAttributeGroup.Attributes != null)
            {
                foreach (XmlSchemaObject attribute in visualAttributeGroup.Attributes)
                {
                    if (attribute.GetType() == typeof(XmlSchemaVisualAttribute))
                    {
                        XmlSchemaObject attr = attribute;
                        Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                        DesignerItem DIattribute = CreateXmlSchemaVisualAttribute(newPosition, ref attr);

                        if (DIattribute != null)
                        {
                            Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                            connectorDecorator.ApplyTemplate();
                            Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                            Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIattribute) as Control;
                            childConnectorDecorator.ApplyTemplate();

                            Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                            Connection conn = new Connection(bottom, top);
                            Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                            designerCanvas.Children.Add(conn);
                            ++i;
                        }
                    }
                    else if (attribute.GetType() == typeof(XmlSchemaVisualAttributeGroupRef))
                    {
                        // create just the connection to the existing attributeGroup (this group exists in document),
                        // not necessarily in our object model (it might not be created yet)

                    }
                }
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaVisualComplexType(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualComplexType.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualComplexType.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualComplexType visualComplexType = element as XmlSchemaVisualComplexType;

            ComplexTypeContent content = new ComplexTypeContent();

            content.Block = "Block: " + visualComplexType.SchemaComplexType.Block;
            content.ContentType = "ContentType: " + visualComplexType.SchemaComplexType.ContentType.ToString();

            if (visualComplexType.SchemaComplexType.Datatype != null)
                content.DataType = "DataType: " + visualComplexType.SchemaComplexType.Datatype.TypeCode.ToString();
            else
                content.DisableContentControl("PART_DataType");

            content.DerivedBy = "DerivedBy: " + visualComplexType.SchemaComplexType.DerivedBy.ToString();
            content.Final = "Final: " + visualComplexType.SchemaComplexType.Final.ToString();
            content.Name = "Name: " + visualComplexType.SchemaComplexType.Name;
            content.Id = "Id: " + visualComplexType.SchemaComplexType.Id;
            content.IsAbstract = "IsAbstract: " + visualComplexType.SchemaComplexType.IsAbstract.ToString();
            content.IsMixed = "IsMixed: " + visualComplexType.SchemaComplexType.IsMixed.ToString();
            content.LineNumber = "LineNumber: " + visualComplexType.SchemaComplexType.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualComplexType.SchemaComplexType.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualComplexType.SchemaComplexType.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualComplexType.SchemaComplexType.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualComplexType.SchemaComplexType.Parent.GetType().ToString();
            content.QualifiedName = "QualifiedName: " + visualComplexType.SchemaComplexType.QualifiedName.Name;
            content.SourceUri = "SourceUri: " + visualComplexType.SchemaComplexType.SourceUri;
            content.TypeCode = "TypeCode: " + visualComplexType.SchemaComplexType.TypeCode.ToString();

            if (visualComplexType.SchemaComplexType.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualComplexType.SchemaComplexType.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (visualComplexType.Annotation != null)
            {
                XmlSchemaObject annotation = visualComplexType.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100 + 30.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaElement(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualElement.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualElement.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualElement visualElement = element as XmlSchemaVisualElement;

            ElementContent content = new ElementContent();

            content.Block = "Block: " + visualElement.SchemaElement.Block.ToString();

            if (visualElement.SchemaElement.Constraints != null)
            {
                content.Constraints = new ObservableCollection<Items>();
                // to do: maybe create new object, not inside this one
            }
            else
            {
                content.DisableContentControl("PART_Constraints");
            }

            content.DefaultValue = "DefaultValue: " + visualElement.SchemaElement.DefaultValue;
            content.ElementSchemaType = "ElementSchemaType: " + visualElement.SchemaElement.ElementSchemaType;
            content.Final = "Final: " + visualElement.SchemaElement.Final;
            content.FixedValue = "FixedValue: " + visualElement.SchemaElement.FixedValue;
            content.Form = "Form: " + visualElement.SchemaElement.Form.ToString();
            content.Id = "Id: " + visualElement.SchemaElement.Id;
            content.IsAbstract = "IsAbstract: " + visualElement.SchemaElement.IsAbstract.ToString();
            content.IsNillable = "IsNillable: " + visualElement.SchemaElement.IsNillable.ToString();
            content.LineNumber = "LineNumber: " + visualElement.SchemaElement.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualElement.SchemaElement.LinePosition.ToString();
            content.MaxOccurs = "MaxOccurs: " + visualElement.SchemaElement.MaxOccurs.ToString();
            content.MinOccurs = "MinOccurs: " + visualElement.SchemaElement.MinOccurs.ToString();
            content.Name = "Name: " + visualElement.SchemaElement.Name;

            if (visualElement.SchemaElement.Namespaces.Count > 0)
            {
                content.Namespaces = new ObservableCollection<Items>();
                XmlQualifiedName[] names = visualElement.SchemaElement.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualElement.SchemaElement.Parent.GetType().ToString();
            content.QualifiedName = "QualifiedName: " + visualElement.SchemaElement.QualifiedName.Name;
            content.RefName = "RefName: " + visualElement.SchemaElement.RefName.Name;
            if (visualElement.SchemaElement.SchemaType != null)
                content.SchemaType = "SchemaType: " + visualElement.SchemaElement.SchemaType.GetType().ToString();
            else
                content.DisableContentControl("PART_SchemaType");
            content.SchemaTypeName = "SchemaTypeName: " + visualElement.SchemaElement.SchemaTypeName.Name;
            content.SourceUri = "SourceUri: " + visualElement.SchemaElement.SourceUri;
            content.SubstitutionGroup = "SubstitutionGroup: " + visualElement.SchemaElement.SubstitutionGroup.Name;

            if (visualElement.SchemaElement.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualElement.SchemaElement.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (visualElement.Annotation != null)
            {
                XmlSchemaObject annotation = visualElement.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100 + 30.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaGroup(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualGroup.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualGroup.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualGroup visualGroup = element as XmlSchemaVisualGroup;

            GroupContent content = new GroupContent();

            content.Name = "Name: " + visualGroup.SchemaGroup.Name;
            content.Id = "Id: " + visualGroup.SchemaGroup.Id;
            content.LineNumber = "LineNumber: " + visualGroup.SchemaGroup.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualGroup.SchemaGroup.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualGroup.SchemaGroup.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualGroup.SchemaGroup.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualGroup.SchemaGroup.Parent.GetType().ToString();
            content.QualifiedName = "QualifiedName: " + visualGroup.SchemaGroup.QualifiedName.Name;
            content.SourceUri = "SourceUri: " + visualGroup.SchemaGroup.SourceUri;

            if (visualGroup.SchemaGroup.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualGroup.SchemaGroup.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (visualGroup.Annotation != null)
            {
                XmlSchemaObject annotation = visualGroup.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100 + 30.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaNotation(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualNotation.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualNotation.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualNotation visualNotation = element as XmlSchemaVisualNotation;

            NotationContent content = new NotationContent();

            content.Name = "Name: " + visualNotation.SchemaNotation.Name;
            content.Id = "Id: " + visualNotation.SchemaNotation.Id;
            content.LineNumber = "LineNumber: " + visualNotation.SchemaNotation.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualNotation.SchemaNotation.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualNotation.SchemaNotation.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualNotation.SchemaNotation.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualNotation.SchemaNotation.Parent.GetType().ToString();
            content.Public = "Public: " + visualNotation.SchemaNotation.Public;
            content.SourceUri = "SourceUri: " + visualNotation.SchemaNotation.SourceUri;
            content.System = "System: " + visualNotation.SchemaNotation.System;

            if (visualNotation.SchemaNotation.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualNotation.SchemaNotation.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (visualNotation.SchemaNotation.Annotation != null)
            {
                XmlSchemaObject annotation = visualNotation.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100 + 30.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        #region OTHER ELEMENTS' FUNCTIONS

        protected DesignerItem CreateXmlSchemaSimpleType(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualSimpleType.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualSimpleType.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleType visualSimpleType = element as XmlSchemaVisualSimpleType;

            SimpleTypeContent content = new SimpleTypeContent();

            content.DataType = "DataType: " + visualSimpleType.SchemaSimpleType.Datatype.TypeCode.ToString();
            content.DerivedBy = "DerivedBy: " + visualSimpleType.SchemaSimpleType.DerivedBy.ToString();
            content.Final = "Final: " + visualSimpleType.SchemaSimpleType.Final.ToString();
            content.Name = "Name: " + visualSimpleType.SchemaSimpleType.Name;
            content.Id = "Id: " + visualSimpleType.SchemaSimpleType.Id;
            content.IsMixed = "IsMixed: " + visualSimpleType.SchemaSimpleType.IsMixed;
            content.LineNumber = "LineNumber: " + visualSimpleType.SchemaSimpleType.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualSimpleType.SchemaSimpleType.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualSimpleType.SchemaSimpleType.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualSimpleType.SchemaSimpleType.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            if (visualSimpleType.SchemaSimpleType.Parent != null)
                content.Parent = "Parent: " + visualSimpleType.SchemaSimpleType.Parent.GetType().ToString();
            else
                content.DisableContentControl("PART_Parent");

            content.QualifiedName = "QualifiedName: " + visualSimpleType.SchemaSimpleType.QualifiedName;
            content.SourceUri = "SourceUri: " + visualSimpleType.SchemaSimpleType.SourceUri;
            content.TypeCode = "TypeCode: " + visualSimpleType.SchemaSimpleType.TypeCode.ToString();

            if (visualSimpleType.SchemaSimpleType.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualSimpleType.SchemaSimpleType.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;

            // create new visual object for annotation and connect it
            if (visualSimpleType.SchemaSimpleType.Annotation != null)
            {
                XmlSchemaObject annotation = visualSimpleType.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create new visual object for content property, which can be XmlSchemaSimpleTypeUnion, XmlSchemaSimpleTypeList 
            // or XmlSchemaSimpleTypeRestriction
            if (visualSimpleType.Content != null)
            {
                XmlSchemaObject cntnt = visualSimpleType.Content;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIcontent = null;
                if (visualSimpleType.Content.GetType() == typeof(XmlSchemaVisualSimpleTypeUnion))
                {
                    DIcontent = CreateXmlSchemaSimpleTypeUnion(newPosition, ref cntnt);
                }
                else if (visualSimpleType.Content.GetType() == typeof(XmlSchemaVisualSimpleTypeList))
                {
                    DIcontent = CreateXmlSchemaSimpleTypeList(newPosition, ref cntnt);
                }
                else if (visualSimpleType.Content.GetType() == typeof(XmlSchemaVisualSimpleTypeRestriction))
                {
                    DIcontent = CreateXmlSchemaSimpleTypeRestriction(newPosition, ref cntnt);
                }

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIcontent) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);

                ++i;
            }


            return newItem;
        }

        protected DesignerItem CreateXmlSchemaSimpleTypeUnion(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualSimpleTypeUnion.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualSimpleTypeUnion.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleTypeUnion visualSimpleTypeUnion = element as XmlSchemaVisualSimpleTypeUnion;

            SimpleTypeUnionContent content = new SimpleTypeUnionContent();

            content.Id = "Id: " + visualSimpleTypeUnion.SchemaSimpleTypeUnion.Id;
            content.LineNumber = "LineNumber: " + visualSimpleTypeUnion.SchemaSimpleTypeUnion.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualSimpleTypeUnion.SchemaSimpleTypeUnion.LinePosition.ToString();

            content.MemberTypes = new ObservableCollection<Items>();
            if (visualSimpleTypeUnion.SchemaSimpleTypeUnion.MemberTypes != null)
            {
                XmlQualifiedName[] names = visualSimpleTypeUnion.SchemaSimpleTypeUnion.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.MemberTypes.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_MemberTypes");


            content.Namespaces = new ObservableCollection<Items>();
            if (visualSimpleTypeUnion.SchemaSimpleTypeUnion.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualSimpleTypeUnion.SchemaSimpleTypeUnion.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualSimpleTypeUnion.SchemaSimpleTypeUnion.Parent.GetType().ToString();
            content.SourceUri = "SourceUri: " + visualSimpleTypeUnion.SchemaSimpleTypeUnion.SourceUri;

            if (visualSimpleTypeUnion.SchemaSimpleTypeUnion.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualSimpleTypeUnion.SchemaSimpleTypeUnion.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;

            // create new visual object for annotation and connect it
            if (visualSimpleTypeUnion.SchemaSimpleTypeUnion.Annotation != null)
            {
                XmlSchemaObject annotation = visualSimpleTypeUnion.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100 * i);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create new visual object for base types
            if (visualSimpleTypeUnion.SchemaSimpleTypeUnion.BaseTypes != null)
            {
                foreach (XmlSchemaObject baseType in visualSimpleTypeUnion.BaseTypes)
                {
                    XmlSchemaObject bt = baseType;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIbaseType = CreateXmlSchemaSimpleType(newPosition, ref bt);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIbaseType) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaSimpleTypeList(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualSimpleTypeList.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualSimpleTypeList.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleTypeList visualSimpleTypeList = element as XmlSchemaVisualSimpleTypeList;

            SimpleTypeListContent content = new SimpleTypeListContent();

            content.Id = "Id: " + visualSimpleTypeList.SchemaSimpleTypeList.Id;
            content.ItemTypeName = "ItemTypeName: " + visualSimpleTypeList.SchemaSimpleTypeList.ItemTypeName.Name;
            content.LineNumber = "LineNumber: " + visualSimpleTypeList.SchemaSimpleTypeList.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualSimpleTypeList.SchemaSimpleTypeList.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualSimpleTypeList.SchemaSimpleTypeList.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualSimpleTypeList.SchemaSimpleTypeList.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + visualSimpleTypeList.SchemaSimpleTypeList.Parent.GetType().ToString();
            content.SourceUri = "SourceUri: " + visualSimpleTypeList.SchemaSimpleTypeList.SourceUri;

            if (visualSimpleTypeList.SchemaSimpleTypeList.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualSimpleTypeList.SchemaSimpleTypeList.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;
            // create new visual object for annotation and connect it
            if (visualSimpleTypeList.SchemaSimpleTypeList.Annotation != null)
            {
                XmlSchemaObject annotation = visualSimpleTypeList.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create new visual object for BaseItemType
            if (visualSimpleTypeList.SchemaSimpleTypeList.BaseItemType != null)
            {
                XmlSchemaObject baseItemType = visualSimpleTypeList.BaseItemType;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIbaseItemType = CreateXmlSchemaSimpleType(newPosition, ref baseItemType);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIbaseItemType) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaSimpleTypeRestriction(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                    FindResource(XmlSchemaVisualSimpleTypeRestriction.defaultAppearanceTemplate) as ControlTemplate,
                    FindResource(XmlSchemaVisualSimpleTypeRestriction.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleTypeRestriction visualSimpleTypeRestriction = element as XmlSchemaVisualSimpleTypeRestriction;

            RestrictionContent content = new RestrictionContent();

            content.BaseTypeName = "BaseTypeName: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.BaseTypeName.Name.ToString();
            content.Id = "Id: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Id;
            content.LineNumber = "LineNumber: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            if (visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Parent != null)
                content.Parent = "Parent: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Parent.GetType().ToString();
            else
                content.DisableContentControl("PART_Parent");

            content.SourceUri = "SourceUri: " + visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.SourceUri;

            if (visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;

            // create new visual object for annotation and connect it
            if (visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Annotation != null)
            {
                XmlSchemaObject annotation = visualSimpleTypeRestriction.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100 * i);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);

                ++i;
            }

            // create new visual object for facet and connect it
            if (visualSimpleTypeRestriction.SchemaSimpleTypeRestriction.Facets.Count != 0)
            {

                foreach (XmlSchemaObject facet in visualSimpleTypeRestriction.facets)
                {
                    XmlSchemaObject f = facet;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIfacet = CreateXmlSchemaFacet(newPosition, ref f);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIfacet) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
            }

            return newItem;
        }

        protected DesignerItem CreateXmlSchemaFacet(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualFacet.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualFacet.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualFacet facet = element as XmlSchemaVisualFacet;

            FacetContent content = new FacetContent();

            content.Type = "Type: " + facet.SchemaFacet.GetType().ToString();
            content.Id = "Id: " + facet.SchemaFacet.Id;
            content.IsFixed = "IsFixed: " + facet.SchemaFacet.IsFixed.ToString();
            content.LineNumber = "LineNumber: " + facet.SchemaFacet.LineNumber.ToString();
            content.LinePosition = "LinePosition: " + facet.SchemaFacet.LinePosition.ToString();

            content.Namespaces = new ObservableCollection<Items>();
            if (facet.SchemaFacet.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = facet.SchemaFacet.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            content.Parent = "Parent: " + facet.SchemaFacet.Parent.GetType().ToString();
            content.SourceUri = "SourceUri: " + facet.SchemaFacet.SourceUri;

            if (facet.SchemaFacet.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in facet.SchemaFacet.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            content.Value = "Value: " + facet.SchemaFacet.Value;

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (facet.SchemaFacet.Annotation != null)
            {
                XmlSchemaObject annotation = facet.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        #region TO DO 
        
        /// <summary>
        /// Parent elements: group, complexType, restriction (both simpleContent and complexContent),
        /// extension (both simpleContent and complexContent)
        /// Content: (annotation?,element*)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaAll(Point position, ref XmlSchemaObject element) // DONE
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualAll.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualAll.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAll all = element as XmlSchemaVisualAll;

            AllContent content = new AllContent();

            // Id
            if (all.SchemaAll.Id != null)
                content.Id = "Id: " + all.SchemaAll.Id;
            else
                content.DisableContentControl("PART_Id");

            // MaxOccurs
            content.MaxOccurs = "MaxOccurs: " + all.SchemaAll.MaxOccurs;

            // MinOccurs
            content.MinOccurs = "MinOccurs: " + all.SchemaAll.MinOccurs;

            // LineNumber
            content.LineNumber = "LineNumber: " + all.SchemaAll.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + all.SchemaAll.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (all.SchemaAll.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = all.SchemaAll.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + all.SchemaAll.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + all.SchemaAll.SourceUri;

            // UnhandledAttributes
            if (all.SchemaAll.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in all.SchemaAll.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;

            // create new visual object for annotation and connect it
            if (all.SchemaAll.Annotation != null)
            {
                XmlSchemaObject annotation = all.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            if (all.Items.Count > 0)
            {
                foreach (XmlSchemaObject vElem in all.Items)
                {
                    XmlSchemaObject elem = vElem;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0*i);
                    DesignerItem DIelement = CreateXmlSchemaElement(newPosition, ref elem);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIelement) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: choice, sequence
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaAny(Point position, ref XmlSchemaObject element) // DONE
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualAny.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualAny.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAny any = element as XmlSchemaVisualAny;

            AnyContent content = new AnyContent();

            // Id
            if (any.SchemaAny.Id != null)
                content.Id = "Id: " + any.SchemaAny.Id;
            else
                content.DisableContentControl("PART_Id");

            // MaxOccurs
            content.MaxOccurs = "MaxOccurs: " + any.SchemaAny.MaxOccurs;

            // MinOccurs
            content.MinOccurs = "MinOccurs: " + any.SchemaAny.MinOccurs;

            // Namespace
            content.Namespace = "Namespace: " + any.SchemaAny.Namespace;

            // ProcessContents
            content.ProcessContents = "ProcessContents: " + any.SchemaAny.ProcessContents.ToString();

            // LineNumber
            content.LineNumber = "LineNumber: " + any.SchemaAny.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + any.SchemaAny.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (any.SchemaAny.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = any.SchemaAny.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + any.SchemaAny.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + any.SchemaAny.SourceUri;

            // UnhandledAttributes
            if (any.SchemaAny.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in any.SchemaAny.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (any.SchemaAny.Annotation != null)
            {
                XmlSchemaObject annotation = any.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: complexType, restriction (both simpleContent and complexContent), 
        /// extension (both simpleContent and complexContent), attributeGroup
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaAnyAttribute(Point position, ref XmlSchemaObject element) // DONE
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualAnyAttribute.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualAnyAttribute.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualAnyAttribute anyAttribute = element as XmlSchemaVisualAnyAttribute;

            AnyAttributeContent content = new AnyAttributeContent();

            // Id
            if (anyAttribute.SchemaAnyAttribute.Id != null)
                content.Id = "Id: " + anyAttribute.SchemaAnyAttribute.Id;
            else
                content.DisableContentControl("PART_Id");

            // Namespace
            content.Namespace = "Namespace: " + anyAttribute.SchemaAnyAttribute.Namespace;

            // ProcessContents
            content.ProcessContents = "ProcessContents: " + anyAttribute.SchemaAnyAttribute.ProcessContents.ToString();

            // LineNumber
            content.LineNumber = "LineNumber: " + anyAttribute.SchemaAnyAttribute.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + anyAttribute.SchemaAnyAttribute.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (anyAttribute.SchemaAnyAttribute.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = anyAttribute.SchemaAnyAttribute.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + anyAttribute.SchemaAnyAttribute.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + anyAttribute.SchemaAnyAttribute.SourceUri;

            // UnhandledAttributes
            if (anyAttribute.SchemaAnyAttribute.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in anyAttribute.SchemaAnyAttribute.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (anyAttribute.SchemaAnyAttribute.Annotation != null)
            {
                XmlSchemaObject annotation = anyAttribute.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: complexType
        /// Content: (annotation?,(restriction|extension))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaComplexContent(Point position, ref XmlSchemaObject element) // DONE
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualComplexContent.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualComplexContent.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualComplexContent complexContent = element as XmlSchemaVisualComplexContent;

            ComplexContentContent content = new ComplexContentContent();

            // Id
            if (complexContent.SchemaComplexContent.Id != null)
                content.Id = "Id: " + complexContent.SchemaComplexContent.Id;
            else
                content.DisableContentControl("PART_Id");

            // IsMixed
            content.IsMixed = "IsMixed: " + complexContent.SchemaComplexContent.IsMixed;

            // LineNumber
            content.LineNumber = "LineNumber: " + complexContent.SchemaComplexContent.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + complexContent.SchemaComplexContent.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (complexContent.SchemaComplexContent.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = complexContent.SchemaComplexContent.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + complexContent.SchemaComplexContent.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + complexContent.SchemaComplexContent.SourceUri;

            // UnhandledAttributes
            if (complexContent.SchemaComplexContent.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in complexContent.SchemaComplexContent.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (complexContent.SchemaComplexContent.Annotation != null)
            {
                XmlSchemaObject annotation = complexContent.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            // create new visual object for EXTENSION | RESTRICTION and connect it
            if (complexContent.SchemaComplexContent.Content != null)
            {
                if (complexContent.SchemaComplexContent.Content.GetType() == typeof(XmlSchemaVisualComplexContentExtension))
                {
                    XmlSchemaObject extension = complexContent.Content as XmlSchemaObject;
                    Point newPosition = new Point(position.X, position.Y + 100.0);
                    DesignerItem DIextension = CreateXmlSchemaExtension(newPosition, ref extension);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIextension) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                }
                else if (complexContent.SchemaComplexContent.Content.GetType() == typeof(XmlSchemaVisualComplexContentRestriction))
                {
                    XmlSchemaObject restriction = complexContent.Content;
                    Point newPosition = new Point(position.X, position.Y + 100.0);
                    DesignerItem DIrestriction = CreateXmlSchemaComplexContentRestriction(newPosition, ref restriction);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIrestriction) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                }
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: element, redefine, schema
        /// Content: (annotation?,(simpleContent|complexContent|((group|all|choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaComplexType(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualComplexType.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualComplexType.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualComplexType complexType = element as XmlSchemaVisualComplexType;

            ComplexTypeContent content = new ComplexTypeContent();

            // Id
            if (complexType.SchemaComplexType.Id != null)
                content.Id = "Id: " + complexType.SchemaComplexType.Id;
            else
                content.DisableContentControl("PART_Id");

            // Name
            content.Name = "Name: " + complexType.SchemaComplexType.Name;

            // IsAbstract
            content.IsAbstract = "IsAbstract: " + complexType.SchemaComplexType.IsAbstract.ToString();

            // IsMixed
            content.IsMixed = "IsMixed: " + complexType.SchemaComplexType.IsMixed.ToString();

            // Block
            content.Block = "Block: " + complexType.SchemaComplexType.Block.ToString();

            // Final
            content.Final = "Final: " + complexType.SchemaComplexType.Final.ToString();

            // LineNumber
            content.LineNumber = "LineNumber: " + complexType.SchemaComplexType.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + complexType.SchemaComplexType.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (complexType.SchemaComplexType.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = complexType.SchemaComplexType.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + complexType.SchemaComplexType.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + complexType.SchemaComplexType.SourceUri;

            // UnhandledAttributes
            if (complexType.SchemaComplexType.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in complexType.SchemaComplexType.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            int i = 0;
            // create new visual object for annotation and connect it
            if (complexType.SchemaComplexType.Annotation != null)
            {
                XmlSchemaObject annotation = complexType.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create new visual object for simpleContent or complexContent
            if (complexType.ContentModel != null)
            {
                if (complexType.ContentModel.GetType() == typeof(XmlSchemaVisualComplexContent))
                {
                    XmlSchemaObject complexContent = complexType.ContentModel;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0*i);
                    DesignerItem DIcomplexContent = CreateXmlSchemaComplexContent(newPosition, ref complexContent);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIcomplexContent) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
                else if (complexType.ContentModel.GetType() == typeof(XmlSchemaVisualSimpleContent))
                {
                    XmlSchemaObject simpleContent = complexType.ContentModel;
                    Point newPosition = new Point(position.X, position.Y + 100.0+100.0*i);
                    DesignerItem DIsimpleContent = CreateXmlSchemaSimpleContent(newPosition, ref simpleContent);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIsimpleContent) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
            }

            // create visual object for anyAttribute
            if (complexType.AnyAttribute != null)
            {
                XmlSchemaObject anyAttribute = complexType.AnyAttribute;
                Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                DesignerItem DIanyAttribute = CreateXmlSchemaVisualAttribute(newPosition, ref anyAttribute);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIanyAttribute) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
                ++i;
            }

            // create visual object for attributes or attribute groups
            if (complexType.Attributes.Count > 0)
            {
                foreach (XmlSchemaObject atr in complexType.Attributes)
                {
                    if (atr.GetType() == typeof(XmlSchemaVisualAttribute))
                    {
                        XmlSchemaObject attribute = atr;
                        Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                        DesignerItem DIattribute = CreateXmlSchemaVisualAttribute(newPosition, ref attribute);

                        Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                        connectorDecorator.ApplyTemplate();
                        Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                        Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIattribute) as Control;
                        childConnectorDecorator.ApplyTemplate();

                        Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                        Connection conn = new Connection(bottom, top);
                        Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                        designerCanvas.Children.Add(conn);
                        ++i;
                    }
                    else if (atr.GetType() == typeof(XmlSchemaVisualAttributeGroup))
                    {
                        XmlSchemaObject attributeGroup = atr;
                        Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                        DesignerItem DIattributeGroup = CreateXmlSchemaVisualAttributeGroup(newPosition, ref attributeGroup);

                        Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                        connectorDecorator.ApplyTemplate();
                        Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                        Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIattributeGroup) as Control;
                        childConnectorDecorator.ApplyTemplate();

                        Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                        Connection conn = new Connection(bottom, top);
                        Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                        designerCanvas.Children.Add(conn);
                        ++i;
                    }
                }
            }

            // create visual object for group, all, choice, sequence
            if (complexType.ContentTypeParticle != null)
            {
                if (complexType.ContentTypeParticle.GetType() == typeof(XmlSchemaVisualGroup))
                {
                    XmlSchemaObject group = complexType.ContentTypeParticle;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIgroup = CreateXmlSchemaGroup(newPosition, ref group);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIgroup) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
                else if (complexType.ContentTypeParticle.GetType() == typeof(XmlSchemaVisualAll))
                {
                    XmlSchemaObject all = complexType.ContentTypeParticle;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIall = CreateXmlSchemaAll(newPosition, ref all);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIall) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
                else if (complexType.ContentTypeParticle.GetType() == typeof(XmlSchemaVisualChoice))
                {
                    XmlSchemaObject choice = complexType.ContentTypeParticle;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIchoice = CreateXmlSchemaChoice(newPosition, ref choice);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIchoice) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
                else if (complexType.ContentTypeParticle.GetType() == typeof(XmlSchemaVisualSequence))
                {
                    XmlSchemaObject sequence = complexType.ContentTypeParticle;
                    Point newPosition = new Point(position.X, position.Y + 100.0 + 100.0 * i);
                    DesignerItem DIsequence = CreateXmlSchemaSequence(newPosition, ref sequence);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIsequence) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                    ++i;
                }
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: simpleContent, complexContent 
        /// Content: (annotation?,((group|all|choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?)))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaExtension(Point position, ref XmlSchemaObject element)
        {
            if (element.GetType() == typeof(XmlSchemaVisualComplexContentExtension))
            {
                // ComplexContentExtension

                DesignerItem newItem = new DesignerItem(
                         FindResource(XmlSchemaVisualComplexContentExtension.defaultAppearanceTemplate) as ControlTemplate,
                         FindResource(XmlSchemaVisualComplexContentExtension.defaultConnectorTemplate) as ControlTemplate);
                newItem.Width = 140;

                XmlSchemaVisualComplexContentExtension complexContentExtension = element as XmlSchemaVisualComplexContentExtension;

                ExtensionContent content = new ExtensionContent();

                // Id
                if (complexContentExtension.SchemaComplexContentExtension.Id != null)
                    content.Id = "Id: " + complexContentExtension.SchemaComplexContentExtension.Id;
                else
                    content.DisableContentControl("PART_Id");

                // Base
                content.Base = "Base: " + complexContentExtension.SchemaComplexContentExtension.BaseTypeName.Name;

                // LineNumber
                content.LineNumber = "LineNumber: " + complexContentExtension.SchemaComplexContentExtension.LineNumber.ToString();

                // LinePosition
                content.LinePosition = "LinePosition: " + complexContentExtension.SchemaComplexContentExtension.LinePosition.ToString();

                // Namespaces
                content.Namespaces = new ObservableCollection<Items>();
                if (complexContentExtension.SchemaComplexContentExtension.Namespaces.Count > 0)
                {
                    XmlQualifiedName[] names = complexContentExtension.SchemaComplexContentExtension.Namespaces.ToArray();
                    foreach (XmlQualifiedName name in names)
                    {
                        content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                    }
                }
                else
                    content.DisableContentControl("PART_Namespaces");

                // Parent
                content.Parent = "Parent: " + complexContentExtension.SchemaComplexContentExtension.Parent.GetType().ToString();

                // SourceUri
                content.SourceUri = "SourceUri: " + complexContentExtension.SchemaComplexContentExtension.SourceUri;

                // UnhandledAttributes
                if (complexContentExtension.SchemaComplexContentExtension.UnhandledAttributes != null)
                {
                    content.UnhandledAttributes = new ObservableCollection<Items>();
                    foreach (XmlAttribute attr in complexContentExtension.SchemaComplexContentExtension.UnhandledAttributes)
                    {
                        content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                    }
                }
                else
                {
                    content.DisableContentControl("PART_UnhandledAttributes");
                }

                newItem.Content = content;
                DesignerCanvas.SetLeft(newItem, position.X);
                DesignerCanvas.SetTop(newItem, position.Y);

                Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
                designerCanvas.Children.Add(newItem);
                designerCanvas.SetConnectorDecoratorTemplate(newItem);

                // create new visual object for annotation and connect it
                if (complexContentExtension.SchemaComplexContentExtension.Annotation != null)
                {
                    XmlSchemaObject annotation = complexContentExtension.Annotation;
                    Point newPosition = new Point(position.X, position.Y + 100.0);
                    DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                }
                return newItem;
            }
            else
            {
                // simpleContentExtension

                DesignerItem newItem = new DesignerItem(
                         FindResource(XmlSchemaVisualSimpleContentExtension.defaultAppearanceTemplate) as ControlTemplate,
                         FindResource(XmlSchemaVisualSimpleContentExtension.defaultConnectorTemplate) as ControlTemplate);
                newItem.Width = 140;

                XmlSchemaVisualSimpleContentExtension complexContentExtension = element as XmlSchemaVisualSimpleContentExtension;

                ExtensionContent content = new ExtensionContent();

                // Id
                if (complexContentExtension.SchemaSimpleContentExtension.Id != null)
                    content.Id = "Id: " + complexContentExtension.SchemaSimpleContentExtension.Id;
                else
                    content.DisableContentControl("PART_Id");

                // Base
                content.Base = "Base: " + complexContentExtension.SchemaSimpleContentExtension.BaseTypeName.Name;

                // LineNumber
                content.LineNumber = "LineNumber: " + complexContentExtension.SchemaSimpleContentExtension.LineNumber.ToString();

                // LinePosition
                content.LinePosition = "LinePosition: " + complexContentExtension.SchemaSimpleContentExtension.LinePosition.ToString();

                // Namespaces
                content.Namespaces = new ObservableCollection<Items>();
                if (complexContentExtension.SchemaSimpleContentExtension.Namespaces.Count > 0)
                {
                    XmlQualifiedName[] names = complexContentExtension.SchemaSimpleContentExtension.Namespaces.ToArray();
                    foreach (XmlQualifiedName name in names)
                    {
                        content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                    }
                }
                else
                    content.DisableContentControl("PART_Namespaces");

                // Parent
                content.Parent = "Parent: " + complexContentExtension.SchemaSimpleContentExtension.Parent.GetType().ToString();

                // SourceUri
                content.SourceUri = "SourceUri: " + complexContentExtension.SchemaSimpleContentExtension.SourceUri;

                // UnhandledAttributes
                if (complexContentExtension.SchemaSimpleContentExtension.UnhandledAttributes != null)
                {
                    content.UnhandledAttributes = new ObservableCollection<Items>();
                    foreach (XmlAttribute attr in complexContentExtension.SchemaSimpleContentExtension.UnhandledAttributes)
                    {
                        content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                    }
                }
                else
                {
                    content.DisableContentControl("PART_UnhandledAttributes");
                }

                newItem.Content = content;
                DesignerCanvas.SetLeft(newItem, position.X);
                DesignerCanvas.SetTop(newItem, position.Y);

                Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
                designerCanvas.Children.Add(newItem);
                designerCanvas.SetConnectorDecoratorTemplate(newItem);

                // create new visual object for annotation and connect it
                if (complexContentExtension.SchemaSimpleContentExtension.Annotation != null)
                {
                    XmlSchemaObject annotation = complexContentExtension.Annotation;
                    Point newPosition = new Point(position.X, position.Y + 100.0);
                    DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                    Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                    connectorDecorator.ApplyTemplate();
                    Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                    Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                    childConnectorDecorator.ApplyTemplate();

                    Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                    Connection conn = new Connection(bottom, top);
                    Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                    designerCanvas.Children.Add(conn);
                }
                
                return newItem;
            }
            
        }

        /// <summary>
        /// Parent elements: key, keyref, unique 
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaField(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                      FindResource(XmlSchemaVisualXPath.defaultAppearanceTemplate) as ControlTemplate,
                      FindResource(XmlSchemaVisualXPath.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualXPath field = element as XmlSchemaVisualXPath;

            FieldContent content = new FieldContent();

            // Id
            if (field.SchemaXPath.Id != null)
                content.Id = "Id: " + field.SchemaXPath.Id;
            else
                content.DisableContentControl("PART_Id");

            // XPath
            content.XPath = "IsMixed: " + field.SchemaXPath.XPath;

            // LineNumber
            content.LineNumber = "LineNumber: " + field.SchemaXPath.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + field.SchemaXPath.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (field.SchemaXPath.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = field.SchemaXPath.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + field.SchemaXPath.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + field.SchemaXPath.SourceUri;

            // UnhandledAttributes
            if (field.SchemaXPath.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in field.SchemaXPath.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (field.SchemaXPath.Annotation != null)
            {
                XmlSchemaObject annotation = field.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: group, choice, sequence, complexType, restriction (both simpleContent and complexContent),
        /// extension (both simpleContent and complexContent)
        /// Content: (annotation?,(element|group|choice|sequence|any)*)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaChoice(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualChoice.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualChoice.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualChoice choice = element as XmlSchemaVisualChoice;

            ChoiceContent content = new ChoiceContent();

            // Id
            if (choice.SchemaChoice.Id != null)
                content.Id = "Id: " + choice.SchemaChoice.Id;
            else
                content.DisableContentControl("PART_Id");

            // MaxOccurs
            content.MaxOccurs = "MaxOccurs: " + choice.SchemaChoice.MaxOccursString;

            // MinOccurs
            content.MinOccurs = "MinOccurs: " + choice.SchemaChoice.MinOccursString;

            // LineNumber
            content.LineNumber = "LineNumber: " + choice.SchemaChoice.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + choice.SchemaChoice.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (choice.SchemaChoice.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = choice.SchemaChoice.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + choice.SchemaChoice.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + choice.SchemaChoice.SourceUri;

            // UnhandledAttributes
            if (choice.SchemaChoice.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in choice.SchemaChoice.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (choice.SchemaChoice.Annotation != null)
            {
                XmlSchemaObject annotation = choice.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: schema
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaImport(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualImport.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualImport.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualImport import = element as XmlSchemaVisualImport;

            ImportContent content = new ImportContent();

            // Id
            if (import.SchemaImport.Id != null)
                content.Id = "Id: " + import.SchemaImport.Id;
            else
                content.DisableContentControl("PART_Id");

            // Namespace
            content.Namespace = "Namespace: " + import.SchemaImport.Namespace;

            // SchemaLocation
            content.SchemaLocation = "SchemaLocation: " + import.SchemaImport.SchemaLocation;

            // LineNumber
            content.LineNumber = "LineNumber: " + import.SchemaImport.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + import.SchemaImport.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (import.SchemaImport.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = import.SchemaImport.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + import.SchemaImport.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + import.SchemaImport.SourceUri;

            // UnhandledAttributes
            if (import.SchemaImport.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in import.SchemaImport.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (import.SchemaImport.Annotation != null)
            {
                XmlSchemaObject annotation = import.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: schema
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaInclude(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualInclude.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualInclude.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualInclude include = element as XmlSchemaVisualInclude;

            IncludeContent content = new IncludeContent();

            // Id
            if (include.SchemaInclude.Id != null)
                content.Id = "Id: " + include.SchemaInclude.Id;
            else
                content.DisableContentControl("PART_Id");

            // SchemaLocation
            content.SchemaLocation = "SchemaLocation: " + include.SchemaInclude.SchemaLocation;

            // LineNumber
            content.LineNumber = "LineNumber: " + include.SchemaInclude.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + include.SchemaInclude.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (include.SchemaInclude.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = include.SchemaInclude.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + include.SchemaInclude.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + include.SchemaInclude.SourceUri;

            // UnhandledAttributes
            if (include.SchemaInclude.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in include.SchemaInclude.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (include.SchemaInclude.Annotation != null)
            {
                XmlSchemaObject annotation = include.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: element
        /// Content: (annotation?,(selector,field+))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaKey(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualKey.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualKey.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualKey key = element as XmlSchemaVisualKey;

            KeyContent content = new KeyContent();

            // Id
            if (key.SchemaKey.Id != null)
                content.Id = "Id: " + key.SchemaKey.Id;
            else
                content.DisableContentControl("PART_Id");

            // Name
            content.Name = "Name: " + key.SchemaKey.Name;

            // LineNumber
            content.LineNumber = "LineNumber: " + key.SchemaKey.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + key.SchemaKey.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (key.SchemaKey.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = key.SchemaKey.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + key.SchemaKey.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + key.SchemaKey.SourceUri;

            // UnhandledAttributes
            if (key.SchemaKey.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in key.SchemaKey.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (key.SchemaKey.Annotation != null)
            {
                XmlSchemaObject annotation = key.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: element
        /// Content: (annotation?,(selector,field+))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaKeyRef(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualKeyref.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualKeyref.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualKeyref complexContent = element as XmlSchemaVisualKeyref;

            KeyRefContent keyref = new KeyRefContent();

            // Id
            if (complexContent.SchemaKeyref.Id != null)
                keyref.Id = "Id: " + complexContent.SchemaKeyref.Id;
            else
                keyref.DisableContentControl("PART_Id");

            // Name
            keyref.Name = "Name: " + complexContent.SchemaKeyref.Name;

            // Refer
            keyref.Refer = "Refer: " + complexContent.SchemaKeyref.Refer;

            // LineNumber
            keyref.LineNumber = "LineNumber: " + complexContent.SchemaKeyref.LineNumber.ToString();

            // LinePosition
            keyref.LinePosition = "LinePosition: " + complexContent.SchemaKeyref.LinePosition.ToString();

            // Namespaces
            keyref.Namespaces = new ObservableCollection<Items>();
            if (complexContent.SchemaKeyref.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = complexContent.SchemaKeyref.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    keyref.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                keyref.DisableContentControl("PART_Namespaces");

            // Parent
            keyref.Parent = "Parent: " + complexContent.SchemaKeyref.Parent.GetType().ToString();

            // SourceUri
            keyref.SourceUri = "SourceUri: " + complexContent.SchemaKeyref.SourceUri;

            // UnhandledAttributes
            if (complexContent.SchemaKeyref.UnhandledAttributes != null)
            {
                keyref.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in complexContent.SchemaKeyref.UnhandledAttributes)
                {
                    keyref.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                keyref.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = keyref;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (complexContent.SchemaKeyref.Annotation != null)
            {
                XmlSchemaObject annotation = complexContent.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: schema
        /// Content: (annotation|(simpleType|complexType|group|attributeGroup))*
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaRedefine(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualRedefine.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualRedefine.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualRedefine schemaRedefine = element as XmlSchemaVisualRedefine;

            RedefineContent redefine = new RedefineContent();

            // Id
            if (schemaRedefine.SchemaRedefine.Id != null)
                redefine.Id = "Id: " + schemaRedefine.SchemaRedefine.Id;
            else
                redefine.DisableContentControl("PART_Id");

            // SchemaLocation
            redefine.SchemaLocation = "SchemaLocation: " + schemaRedefine.SchemaRedefine.SchemaLocation;

            // LineNumber
            redefine.LineNumber = "LineNumber: " + schemaRedefine.SchemaRedefine.LineNumber.ToString();

            // LinePosition
            redefine.LinePosition = "LinePosition: " + schemaRedefine.SchemaRedefine.LinePosition.ToString();

            // Namespaces
            redefine.Namespaces = new ObservableCollection<Items>();
            if (schemaRedefine.SchemaRedefine.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = schemaRedefine.SchemaRedefine.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    redefine.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                redefine.DisableContentControl("PART_Namespaces");

            // Parent
            redefine.Parent = "Parent: " + schemaRedefine.SchemaRedefine.Parent.GetType().ToString();

            // SourceUri
            redefine.SourceUri = "SourceUri: " + schemaRedefine.SchemaRedefine.SourceUri;

            // UnhandledAttributes
            if (schemaRedefine.SchemaRedefine.UnhandledAttributes != null)
            {
                redefine.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in schemaRedefine.SchemaRedefine.UnhandledAttributes)
                {
                    redefine.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                redefine.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = redefine;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            return newItem;
        }
        
        /// <summary>
        /// Parent elements: complexContent
        /// Content: (annotation?,(group|all|choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaComplexContentRestriction(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualComplexContentRestriction.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualComplexContentRestriction.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualComplexContentRestriction restriction = element as XmlSchemaVisualComplexContentRestriction;

            RestrictionContent content = new RestrictionContent();

            // Id
            if (restriction.SchemaComplexContentRestriction.Id != null)
                content.Id = "Id: " + restriction.SchemaComplexContentRestriction.Id;
            else
                content.DisableContentControl("PART_Id");

            // Base
            content.BaseTypeName = "BaseTypeName: " + restriction.SchemaComplexContentRestriction.BaseTypeName;

            // LineNumber
            content.LineNumber = "LineNumber: " + restriction.SchemaComplexContentRestriction.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + restriction.SchemaComplexContentRestriction.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (restriction.SchemaComplexContentRestriction.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = restriction.SchemaComplexContentRestriction.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + restriction.SchemaComplexContentRestriction.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + restriction.SchemaComplexContentRestriction.SourceUri;

            // UnhandledAttributes
            if (restriction.SchemaComplexContentRestriction.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in restriction.SchemaComplexContentRestriction.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (restriction.SchemaComplexContentRestriction.Annotation != null)
            {
                XmlSchemaObject annotation = restriction.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: simpleContent
        /// Content: (annotation?,(simpleType?,(minExclusive|minInclusive|maxExclusive|maxInclusive|totalDigits|fractionDigits|length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?,
        /// ((attribute|attributeGroup)*,anyAttribute?))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaSimpleContentRestriction(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualSimpleContentRestriction.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualSimpleContentRestriction.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleContentRestriction restriction = element as XmlSchemaVisualSimpleContentRestriction;

            RestrictionContent content = new RestrictionContent();

            // Id
            if (restriction.SchemaSimpleContentRestriction.Id != null)
                content.Id = "Id: " + restriction.SchemaSimpleContentRestriction.Id;
            else
                content.DisableContentControl("PART_Id");

            // Base
            content.BaseTypeName = "BaseTypeName: " + restriction.SchemaSimpleContentRestriction.BaseTypeName;

            // LineNumber
            content.LineNumber = "LineNumber: " + restriction.SchemaSimpleContentRestriction.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + restriction.SchemaSimpleContentRestriction.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (restriction.SchemaSimpleContentRestriction.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = restriction.SchemaSimpleContentRestriction.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + restriction.SchemaSimpleContentRestriction.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + restriction.SchemaSimpleContentRestriction.SourceUri;

            // UnhandledAttributes
            if (restriction.SchemaSimpleContentRestriction.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in restriction.SchemaSimpleContentRestriction.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (restriction.SchemaSimpleContentRestriction.Annotation != null)
            {
                XmlSchemaObject annotation = restriction.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: key, keyref, unique
        /// Content: (annotation?)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaSelector(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualXPath.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualXPath.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualXPath selector = element as XmlSchemaVisualXPath;

            SelectorContent content = new SelectorContent();

            // Id
            if (selector.SchemaXPath.Id != null)
                content.Id = "Id: " + selector.SchemaXPath.Id;
            else
                content.DisableContentControl("PART_Id");

            // XPath
            content.XPath = "XPath: " + selector.SchemaXPath.XPath;

            // LineNumber
            content.LineNumber = "LineNumber: " + selector.SchemaXPath.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + selector.SchemaXPath.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (selector.SchemaXPath.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = selector.SchemaXPath.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + selector.SchemaXPath.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + selector.SchemaXPath.SourceUri;

            // UnhandledAttributes
            if (selector.SchemaXPath.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in selector.SchemaXPath.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (selector.SchemaXPath.Annotation != null)
            {
                XmlSchemaObject annotation = selector.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: group, choice, sequence, complexType, restriction (both simpleContent and complexContent),
        /// extension (both simpleContent and complexContent)
        /// Content: (annotation?,(element|group|choice|sequence|any)*)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaSequence(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualSequence.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualSequence.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSequence sequence = element as XmlSchemaVisualSequence;

            SequenceContent content = new SequenceContent();

            // Id
            if (sequence.SchemaSequence.Id != null)
                content.Id = "Id: " + sequence.SchemaSequence.Id;
            else
                content.DisableContentControl("PART_Id");

            // MaxOccurs
            content.MaxOccurs = "MaxOccurs: " + sequence.SchemaSequence.MaxOccurs;

            // MinOccurs
            content.MinOccurs = "MinOccurs: " + sequence.SchemaSequence.MinOccurs;

            // LineNumber
            content.LineNumber = "LineNumber: " + sequence.SchemaSequence.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + sequence.SchemaSequence.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (sequence.SchemaSequence.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = sequence.SchemaSequence.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + sequence.SchemaSequence.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + sequence.SchemaSequence.SourceUri;

            // UnhandledAttributes
            if (sequence.SchemaSequence.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in sequence.SchemaSequence.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (sequence.SchemaSequence.Annotation != null)
            {
                XmlSchemaObject annotation = sequence.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: NONE
        /// Content: ((include|import|redefine|annotation)*,(((simpleType|complexType|group|attributeGroup)|element|attribute|notation),annotation*)*)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaSchema(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisual.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisual.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisual schema = element as XmlSchemaVisual;

            SchemaContent content = new SchemaContent();

            // Id
            if (schema.Schema.Id != null)
                content.Id = "Id: " + schema.Schema.Id;
            else
                content.DisableContentControl("PART_Id");

            // AttributeFormDefault
            content.AttributeFormDefault = "AttributeFormDefault: " + schema.Schema.AttributeFormDefault;

            // ElementFormDefault
            content.ElementFormDefault = "ElementFormDefault: " + schema.Schema.ElementFormDefault;

            // BlockDefault
            content.BlockDefault = "BlockDefault: " + schema.Schema.BlockDefault;
            
            // FinalDefault
            content.FinalDefault = "FinalDefault: " + schema.Schema.FinalDefault;

            // TargetNamespace
            content.TargetNamespace = "TargetNamespace: " + schema.Schema.TargetNamespace;

            // Version
            content.Version = "Version: " + schema.Schema.Version;

            // Xmlns
            content.DisableContentControl("PART_Xmlns");
            
            // LineNumber
            content.LineNumber = "LineNumber: " + schema.Schema.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + schema.Schema.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (schema.Schema.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = schema.Schema.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.DisableContentControl("PART_Parent");

            // SourceUri
            content.SourceUri = "SourceUri: " + schema.Schema.SourceUri;

            // UnhandledAttributes
            if (schema.Schema.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in schema.Schema.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            return newItem;
        }

        /// <summary>
        /// Parent elements: complexType
        /// Content: (annotation?,(restriction|extension))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaSimpleContent(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualSimpleContent.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualSimpleContent.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualSimpleContent simpleContent = element as XmlSchemaVisualSimpleContent;

            SimpleContentContent content = new SimpleContentContent();

            // Id
            if (simpleContent.SchemaSimpleContent.Id != null)
                content.Id = "Id: " + simpleContent.SchemaSimpleContent.Id;
            else
                content.DisableContentControl("PART_Id");

            // LineNumber
            content.LineNumber = "LineNumber: " + simpleContent.SchemaSimpleContent.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + simpleContent.SchemaSimpleContent.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (simpleContent.SchemaSimpleContent.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = simpleContent.SchemaSimpleContent.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + simpleContent.SchemaSimpleContent.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + simpleContent.SchemaSimpleContent.SourceUri;

            // UnhandledAttributes
            if (simpleContent.SchemaSimpleContent.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in simpleContent.SchemaSimpleContent.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (simpleContent.SchemaSimpleContent.Annotation != null)
            {
                XmlSchemaObject annotation = simpleContent.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        /// <summary>
        /// Parent elements: element
        /// Content: (annotation?,(selector,field+))
        /// </summary>
        /// <param name="position"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        protected DesignerItem CreateXmlSchemaUnique(Point position, ref XmlSchemaObject element)
        {
            DesignerItem newItem = new DesignerItem(
                     FindResource(XmlSchemaVisualUnique.defaultAppearanceTemplate) as ControlTemplate,
                     FindResource(XmlSchemaVisualUnique.defaultConnectorTemplate) as ControlTemplate);
            newItem.Width = 140;

            XmlSchemaVisualUnique unique = element as XmlSchemaVisualUnique;

            UniqueContent content = new UniqueContent();

            // Id
            if (unique.SchemaUnique.Id != null)
                content.Id = "Id: " + unique.SchemaUnique.Id;
            else
                content.DisableContentControl("PART_Id");

            // Name
            content.Name = "Name: " + unique.SchemaUnique.Name;

            // LineNumber
            content.LineNumber = "LineNumber: " + unique.SchemaUnique.LineNumber.ToString();

            // LinePosition
            content.LinePosition = "LinePosition: " + unique.SchemaUnique.LinePosition.ToString();

            // Namespaces
            content.Namespaces = new ObservableCollection<Items>();
            if (unique.SchemaUnique.Namespaces.Count > 0)
            {
                XmlQualifiedName[] names = unique.SchemaUnique.Namespaces.ToArray();
                foreach (XmlQualifiedName name in names)
                {
                    content.Namespaces.Add(new DiagramDesigner.Items { InnerText = name.Name + " " + name.Namespace });
                }
            }
            else
                content.DisableContentControl("PART_Namespaces");

            // Parent
            content.Parent = "Parent: " + unique.SchemaUnique.Parent.GetType().ToString();

            // SourceUri
            content.SourceUri = "SourceUri: " + unique.SchemaUnique.SourceUri;

            // UnhandledAttributes
            if (unique.SchemaUnique.UnhandledAttributes != null)
            {
                content.UnhandledAttributes = new ObservableCollection<Items>();
                foreach (XmlAttribute attr in unique.SchemaUnique.UnhandledAttributes)
                {
                    content.UnhandledAttributes.Add(new DiagramDesigner.Items { InnerText = attr.InnerText });
                }
            }
            else
            {
                content.DisableContentControl("PART_UnhandledAttributes");
            }

            newItem.Content = content;
            DesignerCanvas.SetLeft(newItem, position.X);
            DesignerCanvas.SetTop(newItem, position.Y);

            Canvas.SetZIndex(newItem, designerCanvas.Children.Count);
            designerCanvas.Children.Add(newItem);
            designerCanvas.SetConnectorDecoratorTemplate(newItem);

            // create new visual object for annotation and connect it
            if (unique.SchemaUnique.Annotation != null)
            {
                XmlSchemaObject annotation = unique.Annotation;
                Point newPosition = new Point(position.X, position.Y + 100.0);
                DesignerItem DIannotation = CreateXmlSchemaVisualAnnotation(newPosition, ref annotation);

                Control connectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", newItem) as Control;
                connectorDecorator.ApplyTemplate();
                Connector bottom = connectorDecorator.Template.FindName("Bottom", connectorDecorator) as Connector;

                Control childConnectorDecorator = newItem.Template.FindName("PART_ConnectorDecorator", DIannotation) as Control;
                childConnectorDecorator.ApplyTemplate();

                Connector top = childConnectorDecorator.Template.FindName("Top", childConnectorDecorator) as Connector;

                Connection conn = new Connection(bottom, top);
                Canvas.SetZIndex(conn, designerCanvas.Children.Count);
                designerCanvas.Children.Add(conn);
            }

            return newItem;
        }

        #endregion

        #endregion

        public void DrawXsd()
        {
            XsdModel xsdModel = this.appModel.XSDModel;
            XmlSchemaVisual visualSchema = xsdModel.VisualSchema;

            Double x = 5.0;
            Double offsetX = 150.0;

            Double y = 5.0;
            Double offsetY = 50.0;

            int i = 0;

            foreach (XmlSchemaObject obj in visualSchema.Items)
            {
                XmlSchemaObject element = obj;
                DesignerItem newItem = CreateVisualXsdElement(new Point(x + offsetX * i, y), ref element);

                i++;
                if (i > 50) break;
            }
        }

        #endregion

        #region COMMON FUNCTIONS

        public void Draw()
        {
            switch (this.appModel.Mode)
            {
                case (int)docs.XML:
                    DrawXml();
                    break;
                case (int)docs.XSD:
                    DrawXsd();
                    break;
                case (int)docs.DTD:

                    break;
                default:
                    break;
            }
        }

        public void showFile()
        {
            Draw();
            xmlcode.Clear();

            String text = "";
            // fill with the new content
            switch (appModel.Mode)
            {
                case (int)docs.XML:
                    text = this.appModel.XMLModel.GetOuterXml();
                    break;
                case (int)docs.XSD:
                    text = this.appModel.XSDModel.GetOuterXml();
                    break;
                case (int)docs.DTD:
                    break;
                default:
                    break;
            }
            xmlcode.Text = text;
        }

        public void unexpectedShutdownNotify()
        {
            MessageBox.Show("Application is shutting down due to an unexpected error.");
        }

        #endregion

        #endregion

    }

    #region M Y   C O M M A N D S

    public class MyCommands
    {
        private static RoutedUICommand exit;
        private static RoutedUICommand templates;

        static MyCommands()
        {
            // exit command plus shortcut
            InputGestureCollection inputs = new InputGestureCollection();
            inputs.Add(new KeyGesture(Key.Q, ModifierKeys.Control, "Ctrl+Q"));

            exit = new RoutedUICommand("Exit", "Exit", typeof(MyCommands), inputs);

            // templates command
            templates = new RoutedUICommand("Templates", "Templates", typeof(MyCommands), null);
        }

        public static RoutedUICommand Exit
        {
            get { return exit; }
        }

        public static RoutedUICommand Templates
        {
            get { return templates; }
        }
    }

    #endregion
}